import datetime
from time import sleep

from django.core.management.base import NoArgsCommand

from slots.models import Transition
from slots_demo.models import Page, Sponsor, Style, Team, Captain


def _say(msg):
    print '### %s' % msg

def demo_slots():
    # create one page, two sponsors, two styles
    page = Page(name="homepage")
    sponsor1 = Sponsor(name="whiskas", product="cat food")
    sponsor2 = Sponsor(name="O2", product="mobiles")
    style1 = Style(name="easter", style="easter.css")
    style2 = Style(name="christmas", style="christmas.css")

    # save them
    page.save()
    sponsor1.save()
    sponsor2.save()
    style1.save()
    style2.save()

    # first -- no schedule
    _say('page timeline: %s' % page.schedule)

    five_seconds = datetime.timedelta(seconds=5)
    ten_seconds = datetime.timedelta(seconds=10)
    fifteen_seconds = ten_seconds + five_seconds
    # we'll start in 15 seconds time
    start = datetime.datetime.now() + ten_seconds

    # easter lasts for 5 seconds
    page.add_to_schedule(style1, start, start + five_seconds)
    # whiskas sponsor from now until the end of easter
    page.add_to_schedule(sponsor1, start, start + ten_seconds)
    # O2 sponsor from the end of easter for 10 seconds
    page.add_to_schedule(sponsor2, start + ten_seconds,
                         start + fifteen_seconds + five_seconds)
    # christmas starts halway through the O2 sponsorship
    # and lasts for 5 seconds after it finishes
    page.add_to_schedule(style2, start+fifteen_seconds,
                         start+fifteen_seconds+ten_seconds)

    _say('page timeline: %s' % page.schedule)
    sleep(1)
    _say('page timeline for sponsor: %s' % page.schedule_for_model('Sponsor'))
    sleep(1)
    _say('page timeline for style: %s' % page.schedule_for_model('Style'))
    sleep(1)
    _say('current sponsor on page: %s' % page.current_for_model('Sponsor'))
    _say('current style on page: %s' % page.current_for_model('Style'))
    _say('next sponsor on page: %s' % page.next_for_model('Sponsor'))
    _say('next style on page: %s' % page.next_for_model('Style'))
    sleep(5)

    _say('25 seconds of scheduling')
    for s in xrange(25):
        sleep(1)
        print page.current

    _say('deleting %s' % sponsor2)
    sponsor2.delete()
    _say('page timeline: %s' % page.schedule)

    sponsor1.delete()
    style1.delete()
    style2.delete()
    page.delete()

def demo_transitions():
    team1 = Team(name="AFC Wimbledon", sport="football")
    team1.save()
    # don't think coops was ever captain, but I'm rubbish with remembering
    # player names what with being a filthy fairweather fan
    captain1 = Captain(name="Kevin Cooper")
    captain1.save()
    # ditto sidwell!
    captain2 = Captain(name="Lee Sidwell")
    captain2.save()

    five_seconds = datetime.timedelta(seconds=5)
    ten_seconds = datetime.timedelta(seconds=10)
    # we'll start in 5 seconds time
    start = datetime.datetime.now() + five_seconds
    # coops will captain us for five seconds
    Transition.objects.create_transition(team1, captain1, start, "Go on Coops!")
    # then sidders
    Transition.objects.create_transition(team1, captain2, start + five_seconds,
                                         "Sidders! Sidders!")
    # then coops will return from injury
    Transition.objects.create_transition(team1, captain1, start + ten_seconds,
                                         "Yay! Coops is back!!")

    for s in xrange(20):
        sleep(1)
        _say("Team captain is: %s" % team1.captain)

    team1.delete()
    captain1.delete()
    captain2.delete()


def demo():
    _say("DEMOING SLOTS: RELATIONSHIPS WHICH ARE TEMPORARY")
    demo_slots()
    _say("DEMOING TRANSITIONS: RELATIONSHIPS WHICH CHANGE")
    demo_transitions()

class Command(NoArgsCommand):
   help=("demo the slots application")
   def handle_noargs(self,**options):
      demo()
